package com.cx.mall.demo.controller;

import com.cx.mall.common.util.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private StreamBridge streamBridge;

    /**
     * 测试发送带TAG的消息
     */
    @GetMapping("/sendWithTag")
    public ResponseVo sendWithTag(@RequestParam String msg, @RequestParam(defaultValue = "success") String tag) {
        Message<?> message = MessageBuilder.withPayload(msg)
                .setHeader("TAG", tag) // 定义消息头传递 Tag
                .build();
        log.info("发送带TAG的消息: {}, tag: {}", msg, tag);
        streamBridge.send("testOutput-out-0", message);
        return ResponseVo.success("发送成功");
    }

    /**
     * 测试发送唯一消息
     */
    @GetMapping("/sendUnique")
    public ResponseVo sendUnique(@RequestParam String msg) {
        Message<?> message = MessageBuilder.withPayload(msg).build();
        log.info("发送唯一消息: {}", msg);
        streamBridge.send("test3Output-out-0", message);
        return ResponseVo.success("唯一消息发送成功");
    }

    /**
     * 测试发送不同类型的消息
     */
    @GetMapping("/sendDifferentTypes")
    public ResponseVo sendDifferentTypes(@RequestParam String msg) {
        // 发送成功类型消息
        Message<?> successMessage = MessageBuilder.withPayload(msg + "_success")
                .setHeader("TAG", "success")
                .build();
        streamBridge.send("testOutput-out-0", successMessage);
        
        // 发送延迟类型消息
        Message<?> delayMessage = MessageBuilder.withPayload(msg + "_delay")
                .setHeader("TAG", "delay")
                .build();
        streamBridge.send("testOutput-out-0", delayMessage);
        
        // 发送错误类型消息
        Message<?> errorMessage = MessageBuilder.withPayload(msg + "_error")
                .setHeader("TAG", "error")
                .build();
        streamBridge.send("testOutput-out-0", errorMessage);
        
        log.info("发送了三种不同类型的消息");
        return ResponseVo.success("发送了三种不同类型的消息");
    }
}
